package com.simteek.activeMQ.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class AppProducer {
    private static final String url = "tcp://192.168.1.39:61616";
    private static final String topicName = "topic-test";

    public static void main(String[] args) throws JMSException {
        // 创建connectionFactory
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        //创建connection
        Connection connection = connectionFactory.createConnection();
        //启动连接
        connection.start();
        //创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //创建一个目标
        Destination destination = session.createTopic(topicName);
        //创建一个生产者
        MessageProducer producer = session.createProducer(destination);
        for (int i = 0; i < 100; i++) {
            //创建消息
            TextMessage textMessage = session.createTextMessage("消息内容：number-" + i);

            //发布消息
//            producer.send(destination, textMessage);
            producer.send(textMessage);
            System.out.println("send success:" + i);
        }
        //关闭连接
        connection.close();
    }
}
